package com.cby.third.cases.pdf

import android.net.http.SslError
import android.os.Bundle
import android.view.View
import android.webkit.*
import androidx.appcompat.app.AppCompatActivity
import com.cby.code.util.CbyLogUtils
import com.cby.third.cases.R

/**
 * @description: PdfActivity
 *
 * @author ChenBaoyang
 * @date 2021/12/1 16:02
 */
class WebLoadPdfActivity : AppCompatActivity() {

    private lateinit var webView: WebView

    private val pdfUrl = "https://www.gjtool.cn/pdfh5/git.pdf"
    private val pdfPath = "sample.pdf"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_web_load_pdf)
        webView = findViewById(R.id.web_view)

        val settings: WebSettings = webView.settings
        settings.javaScriptEnabled = true
        settings.allowFileAccess = true
        settings.allowFileAccessFromFileURLs = true
        settings.allowUniversalAccessFromFileURLs = true
        settings.loadWithOverviewMode = true
        settings.builtInZoomControls = true
        settings.setSupportZoom(true)
        settings.displayZoomControls = true //不显示缩放按钮

        webView.webViewClient = object : WebViewClient() {
            override fun onPageFinished(view: WebView, url: String) {
                super.onPageFinished(view, url)
                CbyLogUtils.flog("加载完成")
            }

            override fun onReceivedError(
                view: WebView, request: WebResourceRequest,
                error: WebResourceError?
            ) {
                CbyLogUtils.flog("onReceivedError：" + (error?.toString() ?: "网页加载失败"))
            }

            override fun onReceivedSslError(
                view: WebView, handler: SslErrorHandler,
                error: SslError
            ) {
                handler.proceed()
            }
        }
    }

    fun onLoadOnlinePdf(view: View) {
        webView.loadUrl("http://mozilla.github.io/pdf.js/web/viewer.html?file=${pdfUrl}")
    }

    fun onLoadOnlinePdf2(view: View) {
        webView.loadUrl("file:///android_asset/PDFjs/PdfTemplate.html?${pdfUrl}")
    }

    fun onLoadAssertPdf(view: View) {
        webView.loadUrl("file:///android_asset/PDFjs/web/viewer.html?file=../../${pdfPath}");
    }

    fun onLoadAssertPdf2(view: View) {
        webView.loadUrl("file:///android_asset/PDFjs/PdfTemplate.html?../${pdfPath}")
    }
}